<template>
  <div>
    <basic-container>
      <avue-form :option="option"
                 v-model="form"
                 :upload-after="uploadAfter"
                 @tab-click="handleTabClick"
                 @submit="handleSubmit">
      </avue-form>
    </basic-container>
  </div>
</template>

<script>
import option from "/src/const/user/info.js";
import { userApi } from "/src/api/system/user.js";
import func from "/src/utils/func.js";

export default {
  data() {
    return {
      index: 0,
      option: option,
      form: {

      },
      img: "",
      currentUser: {}
    };
  },
  created() {
    this.handleWitch();
  },
  mounted() {
      userApi.getUserInfo().then(res => {
      const user = res.data.data;
      this.currentUser = user;
    });
  },
  methods: {
    handleSubmit(form, done) {
      form.avatar = this.img;
      if (this.index === 0) {
          userApi.update(form).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "修改信息成功!"
            });
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            });
          }
          done();
        }, error => {
          window.console.log(error);
          done();
        })
      } else {
        this.HttpConnection.post("/api-user/users/password", {id : this.currentUser.id, oldPassword : form.oldPassword, newPassword : form.newPassword}).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "修改密码成功!"
            });
          } else {
            this.$message({
              type: "error",
            });
          }
          done();
        }, error => {
          window.console.log(error);
          done();
        })
      }
    },
    handleWitch() {
      if (this.index === 0) {
          userApi.getUserInfo().then(res => {
          const user = res.data.data;
          this.form = user
        });
      }
    },
    handleTabClick(tabs) {
      this.index = func.toInt(tabs.index);
      this.handleWitch();
    },
    uploadAfter(res, done, loading,column) {
      this.img = res;
      done()
      this.$message.success('上传成功')
    }
  }
};
</script>

<style>
</style>
